﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scripting_Language_Framework__2._0_
{
    class adjustTextEntered
    {
        public String adjustTextString(String textToSpeech, String[] words)
        {
            Console.WriteLine("Text to Speech Text (prior to adjustment): " + textToSpeech);
            String[] t2s = textToSpeech.Split(' '); //array used to move gaze act tags
            int i = 0;
            //int j = 1;
            int k = 0;
            int limit = t2s.Length - 1;
            int startIndex = 0;
            int aversionPresent = -1;
            int endIndex = 0; 
            int[] wordCountPerSentence = new int[100];
            int[] aversionPresentInSentences = new int[100];
            int[] fixationPresentInSentences = new int[100];
            
            foreach(string a in t2s)
                Console.WriteLine(a);
            
            while (i <= t2s.Length - 1)
            {
                Console.WriteLine("word being analyzed: " + t2s[i]);
                if (t2s[i].IndexOf("::aversion") != -1)
                {
                    aversionPresent=0;
                    aversionPresentInSentences[k] = 1;
                }
                if (t2s[i].IndexOf(".") != -1 | t2s[i].IndexOf("!") != -1 | t2s[i].IndexOf("?") != -1)
                {
                    endIndex = i;
                    Console.WriteLine("i: " + i);
                    wordCountPerSentence[k] = endIndex - (startIndex+aversionPresent);
                    Console.WriteLine("words per sentence: " + wordCountPerSentence[k] + " = endIndex: " + endIndex + "- startIndex: " + startIndex + " - " + aversionPresent);
                    k++;
                    aversionPresent = -1;
                    if (t2s.Length - 1 != i)
                    {
                        if (t2s[i + 1].IndexOf("::fixation") != -1)
                        {
                            startIndex = i + 2;
                            i = i + 1;
                            Console.WriteLine("startIndex after change: " + startIndex);
                            fixationPresentInSentences[k - 1] = 1;
                        }
                        else
                        { 
                            startIndex = i + 1;
                            fixationPresentInSentences[k - 1] = 0;
                        }
                    }

                }
                else if ((t2s[i].IndexOf(".") == -1 | t2s[i].IndexOf("!") == -1 | t2s[i].IndexOf("?") == -1) & t2s[i].IndexOf("::fixation") != -1)
                {
                    Console.WriteLine("i: " + i);
                    endIndex = i-1;
                    wordCountPerSentence[k] = endIndex - (startIndex + aversionPresent);
                    Console.WriteLine("words per sentence: " + wordCountPerSentence[k] + " = endIndex: " + endIndex + "- startIndex: " + startIndex + " - " + aversionPresent);
                    fixationPresentInSentences[k] = 1;
                    k++;
                    aversionPresent = -1;
                    startIndex = i + 1;
                    
                }
                i++;
            }

            i = 0;
            k = 0;

            foreach (int b in wordCountPerSentence)
                Console.WriteLine(b);

            Queue adjustFixate = new Queue();
            int wordsPriorFixate = 0;
            int count = 0;
            while (i <= t2s.Length - 1)
            { 
                if(wordCountPerSentence[k]==1 & aversionPresentInSentences[k]==1 & fixationPresentInSentences[k]==1)
                {
                    Console.WriteLine("wordCountPerSentence: " + wordCountPerSentence[k]);
                    Console.WriteLine("k: " + k);
                    while (count < 3)
                    {
                        Console.WriteLine("word added: " + t2s[i]);
                        adjustFixate.Enqueue(t2s[i] + " ");
                        i++;
                        count++;
                    }
                    if (i == t2s.Length - 1)
                    {
                        Console.WriteLine("word added: " + t2s[i]);
                        adjustFixate.Enqueue(t2s[i] + " ");
                    }
                    else { }
                    k++;
                    count = 0;
                }
                else if (wordCountPerSentence[k] == 1 & (aversionPresentInSentences[k] == 1 & fixationPresentInSentences[k] == 0) | (aversionPresentInSentences[k] == 0 & fixationPresentInSentences[k] == 1))
                {
                    while (count < 2)
                    {
                        Console.WriteLine("word added: " + t2s[i]);
                        adjustFixate.Enqueue(t2s[i] + " ");
                        i++;
                    }
                    if (i == t2s.Length - 1)
                    {
                        Console.WriteLine("word added: " + t2s[i]);
                        adjustFixate.Enqueue(t2s[i] + " ");
                    }
                    else { }

                    k++;
                    count = 0;
                }
                else if (wordCountPerSentence[k] == 1 & aversionPresentInSentences[k] == 0 & fixationPresentInSentences[k] == 0)
                {
                    Console.WriteLine("word added: " + t2s[i]);
                    adjustFixate.Enqueue(t2s[i] + " ");
                    i++;
                    if (i == t2s.Length - 1)
                    {
                        Console.WriteLine("word added: " + t2s[i]);
                        adjustFixate.Enqueue(t2s[i] + " ");
                    }
                    else { }
                    k++;
                }
                else
                {
                    if (wordCountPerSentence[k] % 2 == 0)
                    {
                        wordsPriorFixate = wordCountPerSentence[k] / 2;
                        Console.WriteLine("word count prior fixate: " + wordsPriorFixate);
                    }
                    else if (wordCountPerSentence[k] % 2 == 1)
                    {
                        wordsPriorFixate = (wordCountPerSentence[k] + 1) / 2;
                        Console.WriteLine("word count prior fixate: " + wordsPriorFixate);
                    }

                    if (aversionPresentInSentences[k] == 1 & fixationPresentInSentences[k] == 1)
                    {
                        for (int x = 0; x < wordsPriorFixate + 1; x++)
                        {
                            Console.WriteLine("word added prior fixate: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        adjustFixate.Enqueue("::fixation ");
                        Console.WriteLine("fixate added");

                        while (t2s[i].IndexOf("::fixation") == -1)
                        {
                            Console.WriteLine("word added after fixate: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        i++;
                        k++;
                    }
                    else if (aversionPresentInSentences[k] == 1 & fixationPresentInSentences[k] == 0)
                    {
                        while (i < wordsPriorFixate + 1)
                        {
                            Console.WriteLine("word added prior fixate: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        adjustFixate.Enqueue("::fixation ");
                        Console.WriteLine("fixate added");

                        while ((t2s[i].IndexOf(".") == -1 | t2s[i].IndexOf("!") == -1 | t2s[i].IndexOf("?") == -1) & t2s.Length - 1 != i)
                        {
                            Console.WriteLine("word added after fixate: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        i++;
                        k++;
                    }
                    else if (aversionPresentInSentences[k] == 0 & fixationPresentInSentences[k] == 1)
                    {
                        while (i < wordsPriorFixate)
                        {
                            Console.WriteLine("word added prior: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        adjustFixate.Enqueue("::fixation ");
                        Console.WriteLine("fixate added");

                        while (t2s[i].IndexOf("::fixation") == -1)
                        {
                            Console.WriteLine("word added after: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        i=i+2;
                        k++;
                    }
                    else if (aversionPresentInSentences[k] == 0 & fixationPresentInSentences[k] == 0)
                    {
                        while (i < wordsPriorFixate)
                        {
                            Console.WriteLine("word added prior: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        adjustFixate.Enqueue("::fixation ");

                        while ((t2s[i].IndexOf(".") == -1 | t2s[i].IndexOf("!") == -1 | t2s[i].IndexOf("?") == -1) & t2s.Length - 1 != i)
                        {
                            Console.WriteLine("word added after: " + t2s[i]);
                            adjustFixate.Enqueue(t2s[i] + " ");
                            i++;
                        }
                        i++;
                        k++;
                    }
                }
            }
            i=0;
            k=0;
            
            Console.WriteLine("after fixate adjustment: ");
            foreach (object c in adjustFixate)
            {
                if (i <= t2s.Length - 1)
                {
                    Console.WriteLine(c);
                    t2s[i] = c.ToString();
                    i++;
                }
            }

            i=0;
            /*
            while (i <= t2s.Length - 1 & j <= t2s.Length - 1)
            {
                //move the aversion gaze act one inside the sentance
                k = i - 1;
                if (t2s[i].IndexOf("::aversion") != -1 & t2s[j].IndexOf("::") == -1 & t2s[j+1].IndexOf("::fixation") == -1 & t2s.Length != 3)
                {
                    temp = t2s[i];
                    t2s[i] = t2s[j];
                    t2s[j] = temp;
                    i += 2;
                    j += 2;
                }
                else
                {
                    i++;
                    j++;
                }
            } */  
                             textToSpeech = "";

                //add the new string to the textToSpeech string
                for (int l = 0; l < t2s.Length; l++)
                {
                    textToSpeech += t2s[l] + " ";
                }
            //)
            return textToSpeech; //return it to be sent to the voice toolkit
        }
    }
}